﻿using ASmile.CodeFactory.DaBLL;
using ASmile.CodeFactory.DaBLL.Builder;
using ASmile.CodeFactory.DBModel;
using ASmile.Helpers;
using ASmile.Win;
using ASmile.Win.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ASmile.CodeFactory
{
    public partial class FrmRemark : FrmBase
    {
        public FrmRemark()
        {
            InitializeComponent();
        }

        RemarkBLL BLLObj { get; } = new RemarkBLL();

        ArgPageData ConfigSearchArg { get; } = new ArgPageData();

        void RefreshConfig()
        {
            Config_PageChanged(1, ucPageNav1.OnePageCount, out int total);
            ucPageNav1.InitiControl(total);
        }

        void RefreshTable()
        {
            var m = dgvConfig.GetSelectedClassData<TConfig>();
            dgvColumn.DataSource = null;
            if (m == null)
            {
                dgvTable.DataSource = null;
                return;
            }
            string tableType = tsCoBoxTableType.Text;
            var ret = BLLObj.GetTables(m.Id, tableType, tsCoBoxHideState.SelectedIndex, tsTxtTableSearch.Text);
            dgvTable.DataSource = ret.data;
        }

        void RefreshField()
        {
            var m = dgvTable.GetSelectedClassData<TTableRemark>();
            if (m == null)
            {
                dgvColumn.DataSource = null;
                return;
            }
            var ret = BLLObj.GetFields(m);
            dgvColumn.DataSource = ret.data;
        }

        private void FrmRemark_Load(object sender, EventArgs e)
        {
            //tsCoBoxTableType.SelectedIndex = 0;
            dgvColumn.SetDoubleBuffered(true);
            dgvTable.SetDoubleBuffered(true);
            dgvConfig.SetDoubleBuffered(true);

            tsCoBoxTableType.ComboBox.BindEnum<EmTableType>();
            tsCoBoxHideState.SelectedIndex = 1;
            RefreshConfig();
        }

        private void Config_PageChanged(int pageNo, int pageSize, out int total)
        {
            ConfigSearchArg.PageNo = pageNo;
            ConfigSearchArg.PageSize = pageSize;
            ConfigSearchArg.SearchVal = tsTxtSearchConfig.Text;
            var ret = BLLObj.GetConfigs(ConfigSearchArg);
            total = ret.count;
            dgvConfig.DataSource = ret.data;
        }

        private void tsBtnSearchConfig_Click(object sender, EventArgs e)
        {
            RefreshConfig();
        }

        private void tsTxtSearchConfig_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshConfig();
            }
        }

        private void dgvConfig_SelectionChangedAndCellClick(object sender, DataGridViewCellEventArgs e)
        {
            RefreshTable();
        }

        private void tsBtnAllField_Click(object sender, EventArgs e)
        {
            if (dgvTable.Rows.Count == 0)
            {
                ShowMessage("没有可同步的表");
                return;
            }

            if (!ShowQuestion("确实要同步所有表字段吗？该过程将持续较长时间", "同步全部确认")) return;

            this.LoadingShow(Run, null, dgvTable.Rows.Count);

            void Run()
            {
                for (int i = 0; i < dgvTable.Rows.Count; i++)
                {
                    var m = dgvTable.GetSelectedClassData<TTableRemark>(i);
                    var ret = BLLObj.SyncField(m);
                    this.LoadingProgressAdd();
                }

                ShowMessage($"同步完成");
            }
        }

        private void tsBtnSyncTable_Click(object sender, EventArgs e)
        {
            var config = dgvConfig.GetSelectedClassData<TConfig>();
            if (config == null)
            {
                ShowMessage("未选中配置");
                return;
            }
            var tableType = tsCoBoxTableType.Text;
            EnumHelper.StringToEnum(tsCoBoxTableType.Text, out EmTableType tbType);
            if (!ShowQuestion($"确实要同步 {tableType} - {config.Caption} 信息吗？", "同步确认")) return;


            this.LoadingShow(Sync);

            void Sync()
            {
                ApiResult ret = BLLObj.SyncDbObject(config, tbType);
                ShowMessage(ret.msg);
                if (!ret)
                {
                    return;
                }
                RefreshTable();
            }

        }

        private void dgvTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var selModel = dgvTable.GetSelectedClassData<TTableRemark>();
            if (selModel == null)
            {
                return;
            }
            tsBtnEditTable_Click(sender, e);
        }

        private void tsBtnEditTable_Click(object sender, EventArgs e)
        {
            var selModel = dgvTable.GetSelectedClassData<TTableRemark>();
            if (selModel == null)
            {
                ShowMessage("未选中记录");
                return;
            }
            FrmRemarkTableEdit frm = new FrmRemarkTableEdit();
            var ret = frm.ShowDialog(new EditFormArgs
            {
                SetModel = selModel,
                SaveHandle = m => BLLObj.SetTableRemark(m as TTableRemark)
            });
            if (ret)
            {
                ShowMessage("设置成功");
                dgvTable.Refresh();
            }
        }

        private void tsCoBoxTableType_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshTable();
        }

        private void tsCoBoxHideState_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshTable();
        }
        private void tsBtnTableSearch_Click(object sender, EventArgs e)
        {
            RefreshTable();
        }

        private void tsTxtTableSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshTable();
            }
        }

        private void dgvTable_SelectionChangedAndCellClick(object sender, DataGridViewCellEventArgs e)
        {
            RefreshField();
        }

        private void tsBtnSyncField_Click(object sender, EventArgs e)
        {
            var m = dgvTable.GetSelectedClassData<TTableRemark>();
            if (m == null)
            {
                ShowMessage("未选中记录");
                return;
            }
            if (!ShowQuestion($"确实要同步 {m.TableName} 字段信息吗？", "同步确认")) return;
            var ret = BLLObj.SyncField(m);
            ShowMessage(ret.msg);
            if (!ret)
            {
                return;
            }
            RefreshField();
        }

        private void tsBtnEditField_Click(object sender, EventArgs e)
        {
            var selModel = dgvColumn.GetSelectedClassData<TFieldRemark>();
            if (selModel == null)
            {
                ShowMessage("未选中记录");
                return;
            }
            FrmRemarkFieldEdit frm = new FrmRemarkFieldEdit();
            var ret = frm.ShowDialog(new EditFormArgs
            {
                SetModel = selModel,
                SaveHandle = m => BLLObj.SetFieldRemark(m as TFieldRemark)
            });
            if (ret)
            {
                ShowMessage("设置成功");
                dgvColumn.Refresh();
            }
        }

        private void dgvColumn_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var selModel = dgvColumn.GetSelectedClassData<TFieldRemark>();
            if (selModel == null)
            {
                return;
            }
            tsBtnEditField_Click(sender, e);
        }

        private void tsBtnFieldSearch_Click(object sender, EventArgs e)
        {
            RefreshField();
        }

        private void tsTxtFieldSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshField();
            }
        }

        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void SetTableHide(bool isHide)
        {
            var m = dgvTable.GetSelectedClassData<TTableRemark>();
            if (m == null)
            {
                ShowMessage("没有选中表记录");
                return;
            }
            var r = BLLObj.SetTableHide(m, isHide);
            ShowMessage(r.msg);
            if (r)
            {
                dgvTable.Refresh();
            }
        }

        void SetFieldHide(bool isHide)
        {
            var m = dgvColumn.GetSelectedClassData<TFieldRemark>();
            if (m == null)
            {
                ShowMessage("没有选中表记录");
                return;
            }
            var r = BLLObj.SetFieldHide(m, isHide);
            ShowMessage(r.msg);
            if (r)
            {
                dgvColumn.Refresh();
            }
        }

        private void tsmiSetHide_Click(object sender, EventArgs e)
        {
            var srcControl = contextMenuStrip1.SourceControl;
            if (srcControl == dgvTable)
            {
                SetTableHide(true);
            }
            else if (srcControl == dgvColumn)
            {
                SetFieldHide(true);
            }
        }

        private void tsmiSetNoHide_Click(object sender, EventArgs e)
        {
            var srcControl = contextMenuStrip1.SourceControl;
            if (srcControl == dgvTable)
            {
                SetTableHide(false);
            }
            else if (srcControl == dgvColumn)
            {
                SetFieldHide(false);
            }
        }

    }
}
